package com.mego.tzadd.database.manager;


import com.mego.tzadd.activitys.AppContext;
import com.mego.tzadd.database.dbmodel.SystemAllInfo;
import com.mego.tzadd.database.greendao.SystemAllInfoDao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

//SystemAllInfo cc = SystemCenter.getInstance().getSysInfo("CCID");

public class SystemCenter {

    private static SystemCenter instance = new SystemCenter();

    public static SystemCenter getInstance() {
        return instance;
    }

    public String wifiname = "";
    public String wifimima = "";
    public long adminid = 0;//当前的用户id
    public String username = "";
    public String email = "";
    public String mobile = "";

    public String ssid = ""; //机台wifi

    public String ssid0 = ""; //原上网连接的wifi

    public SystemAllInfo getSysInfo(String key) {
        if (key == null || key.length() < 1) {
            return null;
        }
        String strContent = "";
        SystemAllInfo sInfo = null;
        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            ArrayList<SystemAllInfo> sysList = new ArrayList<SystemAllInfo>();
            sysList = (ArrayList) systemAllInfoDao.queryBuilder()
                    .where(SystemAllInfoDao.Properties.Name.eq(key))
                    .build().list();
            if (sysList.size() > 0) {
                sInfo = sysList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sInfo;
    }

    //da4320
    public Long saveSysInfo(SystemAllInfo info) {
        if (info == null) {
            return 0L;
        }

        SystemAllInfo a = getSysInfo(info.getName());
        if (a != null) {
            info.setId(a.getId());
        }

        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            long id = systemAllInfoDao.insertOrReplace(info);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    //获取当天发短信的次数
    public static int getSendsmstimes() {
        int t = 0;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        String today = simpleDateFormat.format(date) + "_smstimes";

        SystemAllInfo st = instance.getSysInfo(today);
        if (st != null) {
            t = Integer.parseInt(st.getContent());
        }
        return t;
    }

    //发短信次数++
    public static void addSmstimes() {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        String today = simpleDateFormat.format(date) + "_smstimes";

        int t = 0;
        SystemAllInfo st = instance.getSysInfo(today);
        if (st != null) {
            t = Integer.parseInt(st.getContent()) + 1;
            st.setContent(t + "");
            instance.saveSysInfo(st);
        } else {
            instance.saveSysInfo(new SystemAllInfo(null, today, "1"));
        }
    }


}
